package com.wendu.oa.common.page;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wendu.oa.model.Employee;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageUtils {

    /**
     *
     *  public static <T> XXX fun(){}
     *  此处<T> 表示定义另一个泛型
     *
     */
    public static <T>  Map<String,Object> getPage(PageParam pageParam,QueryAction<T> action){

        /**
         * PageHelper.startPage(employeeDto)作用：对紧随其后的查询修改查数据sql，新增总数查询
         *
         * ThreadLocal
         */
        PageHelper.startPage(pageParam);//设置分页参数，将对紧随其后的查询起作用
        List<T> list = action.query();
        PageInfo<T> pageInfo = new PageInfo<>(list);//该行代码一定要紧随查询之后，封装分页结果

        Map<String,Object> page = new HashMap<>();
        page.put("first",1);
        page.put("pre",pageInfo.getPrePage());
        page.put("next",pageInfo.getNextPage());
        page.put("last",pageInfo.getPages());
        page.put("current",pageInfo.getPageNum());

        page.put("pageSize",pageInfo.getPageSize());
        page.put("total",pageInfo.getTotal());
        page.put("pgTotal",pageInfo.getPages());
        page.put("currSize",pageInfo.getSize());

        page.put("list",pageInfo.getList());



        return page;

    }
}
